<?php	include_once('inc/header.php');
		include_once('inc/product.inc.php');
if(!isset($_GET['cat'])){
echo'		
<div id="banner" style="background-image: url(\'img/headerBg.jpg\'); background-color: black; color:white; background-position: center; background-repeat: no-repeat;">
			<div class="container intro_wrapper">
				<div class="inner_content"></div>
					<div class="col-md-12">
					<h1 style="font-size: 3em; text-transform: uppercase; font-weight: 300;  padding: 0 0 0 0;">Products Browser</h1>
					
					</div>
				</div>
			</div>';}
	echo'<!--page-->
	<div id="banner" style="background-image: url(\'img/softbg.jpg\'); background-color: black; color:white; background-position: center; background-repeat: no-repeat;">
		<div class="container intro_wrapper">
			<div class="inner_content" >
				'; 
				
	$req = $db->query('SELECT cat_id, cat_name FROM jktrade_cat WHERE cat_id>1');
	$data = $req->fetchAll();
	
	echo '<div class="col-md-2"><h1>CATEGORIES</h1></div><div class="col-md-10">';
	
	$reqcat = $db->query('SELECT * FROM jktrade_cat WHERE cat_parentcat=0');

					while ($cat = $reqcat->fetch())
				{		
						//Checking if this category have subcats
						$req = $db->query('SELECT cat_id FROM jktrade_cat WHERE cat_parentcat = '.$cat['cat_id'].'');
						$SubCats = $req->fetch();
						//if it's "ALL" or if there's no sub cats
						if($cat['cat_id']==0 OR !$SubCats){
						echo '
						<a href="browse_products.php?cat='.$cat['cat_id'].'"><button type="button" class="btn btn-link" style="text-transform: uppercase; font-weight: 800; font-size: 1.3em;">'.$cat['cat_name'].'</button></a>
						';
						}
						else{
						echo'<button type="button" class="btn btn-link" style="text-transform: uppercase; font-weight: 800; font-size: 1.3em;" onclick="showSubcat('.$cat['cat_id'].');">'.$cat['cat_name'].'</button>';
						}
				
				}


					echo'
				</div>
			</div></div><div id="subcats"></div></div>';
		


					
				// 			/**********************   Variables recovery  **********************/
$nbCat = count_categories($db); // Quantity of categories present in the database
if (isset($_GET['cat']) && ($_GET['cat'])>=0 && ($_GET['cat'])< $nbCat)
{
	$reqrd_cat = htmlspecialchars($_GET['cat']);
}
else
{
	$reqrd_cat = 0;
}
$cat = check_category($db, $reqrd_cat); // We get back the required cat
$browse_products = true;				
$cats = search_sub_cat($db, $cat);  // We get the sub cat if they exist (if so, products are linked to these sub cat)	
$nbProducts = count_product($db, $cat, $cats); //Quantity of products belonging to $cat
$perPage = 12; // For the now we want to display a maximum of 12 products/page
$nbPages = ceil($nbProducts/$perPage); // Quantity of all pages
//!\\ If $remaining_products < $perPage --> We're on the last page
$query_string = "";	//!\\ Maybe not a good way to do it, mostly because of security issues //!\\			


if (isset($_GET['p']) && ($_GET['p'])>0 && ($_GET['p'])<= $nbPages) // Current page initialisation
{
	$cPage = htmlspecialchars($_GET['p']);
}
else
{
	$cPage = 1;
}

$first_product = $perPage*($cPage-1); //Ranking of the first displayed product on the current page
$remaining_products = $nbProducts-(($cPage-1)*$perPage); //Quantity of products that still have to be displayed
				/**********************   End of declarations  *********************/

				
				
echo'
<div class="container wrapper">
			<div class="inner_content">
<div id="options">                                           
                    ';  //class="option-set" data-option-key="filter"  <li><a href="#filter" data-option-value="*" class=" selected">All</a></li> 
					


//Displaying the separation between the list of categories and the products
echo'	                                          
			<div class="clear"></div>
		</div>';	
		
//Second loop: getting the required products and displaying them
echo'

	<div class="row">      
	<div id="grid-gallery" class="grid-gallery">
				<section class="grid-wrap">
					<ul class="grid" >
						<li class="grid-sizer"></li>
	'; 
	
	/*Getting the products corresponding with the required cat
		Many conditional queries needed for this*/
	
	if(!$cat) //If no specific category is required
	{
		$query_string = "SELECT * FROM jktrade_products ORDER BY pr_name
					LIMIT $first_product,$perPage";
	}
	else if (!$cats) //Selected cat is already a sub-category or doesn't own any sub-cat
	{
		$query_string = "SELECT * FROM jktrade_products WHERE pr_cat = $cat ORDER BY pr_name
							LIMIT $first_product,$perPage";
	}
	else //If there are sub-cats
	{

		$query_string = 'SELECT * FROM jktrade_products WHERE pr_cat = '.$cats[0].' ';
				
		for($i=1; $i<(count($cats)); $i++)
		{
			$query_string .= 'OR pr_cat ='.$cats[$i].' ';
		}
		
		$query_string .= "ORDER BY pr_name LIMIT $first_product,$perPage";
	}
	

	//We create the final query
	$reqpr = $db->query($query_string);
	$i=1;
	//Displaying them
	while ($datapr = $reqpr->fetch())
	{
		echo'
			<!--'.$datapr['pr_id'].'-->
						<li>
							<figure>
							
							<div class="hover_img">
								<img src="img/uploads/products/thumb/'.$datapr['pr_mainpic'].'" alt="img'.$datapr['pr_id'].'"/>
							</div>
							
								<figcaption>
								
									<h3 style="display: table-cell; vertical-align: middle; text-transform: uppercase;"><a href="#">'.$datapr['pr_name'].' </a></h3>
										<p>'. mb_strimwidth($datapr['pr_shortdesc'],0, 100, "...").'</p>
											<h3 style="font-weight: 800; font-size: 1.6em; margin-top: 10px; text-align: right;">'.$datapr['pr_price'].' Kr-</h3>
									</figcaption>
									
							</figure>
						</li>
						
						'; 
						$i++;
	}

echo'	</ul>
				</section>
				<div id="figures" ></div>
			<!-- // grid-wrap -->
				<section class="slideshow">
					<ul>';
			
		$i=1;		
		$reqpr = $db->query($query_string);
		
		while ($datapr = $reqpr->fetch())
	{
		echo'
			<!--'.$datapr['pr_id'].'-->
						<li><div style="background-image: url(\'img/uploads/products/large/'.$datapr['pr_mainpic'].'\'); background-size: 100%; background-position: 50% 50%;  height: 250px"></div>
							<figure>
							
								<figcaption>
									<h3>'.$datapr['pr_name'].'</h3>
									<h3 style="font-weight: 800; font-size: 1.6em;  text-align: right;">'.$datapr['pr_price'].' Kr-</h3>
									<p>
										'.$datapr['pr_shortdesc'].'
									</p>
									<form method="get" action="show_product.php" >
									<input type="hidden" value="'.$datapr['pr_id'].'" name="pr"/>
									<input value="View product" class="btn btn-info btn-danger btn-custom" type="submit">
									</form>
								</figcaption>
								
								
							</figure>
						</li>	
						
						'; 
						$i++;
	}
	
	echo'	</ul>
					<nav>
						<span class="icon nav-prev"></span>
						<span class="icon nav-next"></span>
						<span class="icon nav-close"></span>
					</nav>
					<div class="info-keys icon">Navigate with arrow keys</div>
				</section><!-- // slideshow -->
			</div><!-- // grid-gallery -->
		';

	
//Displaying the separation between the list of categories and the products

			echo'
				
	<div style="position: absolute; left: 0px; top: 0px; transform: translate(1200px, 598px);" class="clear isotope-item"></div>
</div>   
	<!-- //portfolio_block -->   
		<div class="holder">
		
			<a href="browse_products.php?cat='.$cat.'&p=1" >← first</a>';
			
			if($cPage>2)
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			
			if($cPage>1)
			{
				echo '<a href="browse_products.php?cat='.$cat.'&p='.($cPage-1).'">'.($cPage-1).'</a>';
			}
			
			echo '<a href="browse_products.php?cat='.$cat.'&p='.($cPage).'" class="jp-current">'.($cPage).'</a>';
			
			if($cPage < $nbPages)
			{
				echo '<a href="browse_products.php?cat='.$cat.'&p='.($cPage+1).'">'.($cPage+1).'</a>';
			}
			
			if($cPage<($nbPages-1))
			{
				echo '<span style="margin-right:8px" class="jp-hidden">...</span>';
			}
			echo'<a href="browse_products.php?cat='.$cat.'&p='.$nbPages.'">last →</a>
		</div>
	</div>
</div>
</div>

	<div class="pad25 hidden-md hidden-lg"></div>
	<!--//page-->';
    
include_once('inc/footer.php'); ?>